package dailytest;

import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Objects;


/**
 * @author heweiwei
 * @date 2021/7/21 - 8:20 下午
 * @Description:
 */
public class Test {


    public static void main(String[] args) {

        BigDecimal bigDecimal = new BigDecimal("58");
        System.out.println("bigDecimal = " + bigDecimal.scale());
    }

    public static BigDecimal[] stockNumCalculate(BigDecimal num, Integer ratio) {
        if (ObjectUtils.isEmpty(ratio) || ratio.equals(0) || ObjectUtils.isEmpty(num)) {
            BigDecimal[] result = new BigDecimal[2];
            result[0] = new BigDecimal("0");
            result[1] = new BigDecimal("0");
            return result;
        }
        return divideAndRemainder(num, new BigDecimal(ratio.toString()));
    }


    public static BigDecimal[] divideAndRemainder(BigDecimal v1, BigDecimal v2) {
        return v1.divideAndRemainder(v2);
    }

    public static BigDecimal getAmount(BigDecimal price, BigDecimal smallPrice, BigDecimal num, BigDecimal smallNum) {
        BigDecimal bigAmount = mul(price, num == null ? new BigDecimal("0") : num, 2);
        BigDecimal smallAmount = mul(smallPrice, smallNum == null ? new BigDecimal("0") : smallNum, 2);
        return add(bigAmount, smallAmount, 2);
    }



    public static BigDecimal add(BigDecimal v1, BigDecimal v2, int scale) {
        if (scale < 0) {
            return BigDecimal.ZERO;
        } else {
            if (Objects.isNull(v1)) {
                v1 = BigDecimal.ZERO;
            }

            if (Objects.isNull(v2)) {
                v2 = BigDecimal.ZERO;
            }

            return v1.add(v2).setScale(scale, 4);
        }
    }
    public static BigDecimal mul(BigDecimal v1, BigDecimal v2, int scale) {
        if (scale < 0) {
            return new BigDecimal(0);
        } else {
            if (Objects.isNull(v1)) {
                v1 = BigDecimal.ZERO;
            }

            if (Objects.isNull(v2)) {
                v2 = BigDecimal.ZERO;
            }

            return v1.multiply(v2).setScale(scale, 4);
        }
    }

}
